package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
@SuppressWarnings({ "all" })
public class UserListenProcessServiceImpl implements UserListenProcessService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新用户声音播放进度
     *
     * @param userListenProcessVo 用户播放进度信息
     */
    @Override
    public void updateListenProcess(Long userId, UserListenProcessVo userListenProcessVo) {
        // 1. 构建动态集合名:userListenProcess_{userId}
        String collectionName = "userListenProcess_" + userId;

        // 2. 构建查询条件:根据albumId和trackId查找记录
        Query query = new Query();
        query.addCriteria(Criteria.where("albumId").is(userListenProcessVo.getAlbumId())
                .and("trackId").is(userListenProcessVo.getTrackId()));

        // 3. 查询是否已存在记录
        UserListenProcess existingProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
        if (existingProcess != null) {
            // 4. 已存在,更新记录
            Update update = new Update();
            update.set("breakSecond", userListenProcessVo.getBreakSecond());
            update.set("updateTime", new Date());
            update.set("isShow", 1); // 设置为显示
            mongoTemplate.updateFirst(query, update, UserListenProcess.class, collectionName);
            log.info("更新播放进度成功 - userId: {}, albumId: {}, trackId: {}",
                    userId, userListenProcessVo.getAlbumId(), userListenProcessVo.getTrackId());
        } else {
            // 5. 不存在,新增记录
            UserListenProcess newProcess = new UserListenProcess();
            newProcess.setUserId(userId);
            newProcess.setAlbumId(userListenProcessVo.getAlbumId());
            newProcess.setTrackId(userListenProcessVo.getTrackId());
            newProcess.setBreakSecond(userListenProcessVo.getBreakSecond());
            newProcess.setIsShow(1); // 设置为显示
            newProcess.setCreateTime(new Date());
            newProcess.setUpdateTime(new Date());
            mongoTemplate.save(newProcess, collectionName);
            log.info("新增播放进度记录成功 - userId: {}, albumId: {}, trackId: {}",
                    userId, userListenProcessVo.getAlbumId(), userListenProcessVo.getTrackId());
        }
    }

    /**
     * 获取声音的上次播放进度
     *
     * @param trackId 声音ID
     * @return 播放进度（秒），如果没有播放记录返回null
     */
    @Override
    public BigDecimal getTrackBreakSecond(Long userId, Long trackId) {
        log.info("开始查询声音播放进度 - userId: {}, trackId: {}", userId, trackId);
        // 1. 构建动态集合名:userListenProcess_{userId}
        String collectionName = "userListenProcess_" + userId;
        // 2. 构建查询条件:根据trackId查找记录
        Query query = new Query();
        query.addCriteria(Criteria.where("trackId").is(trackId));
        // 3. 查询播放进度记录
        UserListenProcess userListenProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
        if (userListenProcess != null) {
            log.info("查询到播放进度 - userId: {}, trackId: {}, breakSecond: {}",
                    userId, trackId, userListenProcess.getBreakSecond());
            return userListenProcess.getBreakSecond();
        } else {
            log.info("未查询到播放进度记录 - userId: {}, trackId: {}", userId, trackId);
            return BigDecimal.ZERO;
        }
    }

}
